<?php
/*
 * @Descripttion:
 * @version:
 * @Author: ewgof
 * @Date: 2020-08-13 21:28:51
 * @LastEditors: ewgof
 * @LastEditTime: 2020-08-13 22:08:43
 */
namespace cw;

use Medoo\Medoo;

class Db extends Base
{
    protected $config = [];

    public function __construct()
    {
        $this->config = config(config('app.default_db'));
        if (empty($this->config)) {
            throw new RuntimeException(lang(5));
        }
    }


    public function mysql()
    {
        $database = new Medoo([
            // required
            'database_type' => 'mysql',
            'database_name' => $this->config['dbname'],
            'server' => $this->config['host'],
            'username' => $this->config['username'],
            'password' => $this->config['passwd'],
            'port' => $this->config['port'],
         
            // // [optional]
            // 'charset' => 'utf8',
            // 'collation' => 'utf8_general_ci',
         
            // // [optional] Table prefix
            // 'prefix' => '',
         
            // // [optional] Enable logging (Logging is disabled by default for better performance)
            // 'logging' => true,
         
            // // [optional] MySQL socket (shouldn't be used with server and port)
            // 'socket' => '/tmp/mysql.sock',
         
            // // [optional] driver_option for connection, read more from http://www.php.net/manual/en/pdo.setattribute.php
            // 'option' => [
            //     PDO::ATTR_CASE => PDO::CASE_NATURAL
            // ],
         
            // // [optional] Medoo will execute those commands after connected to the database for initialization
            // 'command' => [
            //     'SET SQL_MODE=ANSI_QUOTES'
            // ]
        ]);
        return $database;
    }


    public function mssql()
    {
        $database = new Medoo([
            'database_type' => 'mssql',
            'database_name' => $this->config['dbname'],
            'server' => $this->config['host'],
            'username' => $this->config['username'],
            'password' => $this->config['passwd'],
        ]);
        return $database;
    }


    public function sqlite()
    {
        $database = new Medoo([
            'database_type' => 'sqlite',
            'database_file' => $this->config['db_file'],
        ]);
        return $database;
    }


    
}
